home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / utils / dos / mv.c < prev    next >
C/C++ Source or Header  |  1995-04-18  |  3KB  |  176 lines

  1.  
  2.  
  3. /*
  4.  *
  5.  *    Copyright 1993 Algorithms Corporation
  6.  *
  7.  *    ALL RIGHTS RESERVED.
  8.  *
  9.  *
  10.  *
  11.  */
  12.  
  13.  
  14.  
  15.  
  16.  
  17. #include <stdio.h> 
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <dos.h>
  21.  
  22. static    void    strip_path();
  23. static    void    usage();
  24. static    int    rm();
  25.  
  26.  
  27. int    main(argc, argv)
  28. int    argc;
  29. char    *argv[];
  30. {
  31.     int    r, i, isdir, err=0, quiet=0, force=0, existing=0, verbose=0, zero=0;
  32.     struct    stat    sb;
  33.     char    buf[128], tmp[128];
  34.     
  35.     if (argc < 3)
  36.         usage();
  37.     strip_path(tmp, argv[argc-1]);
  38.     if (!*tmp  ||  !strcmp(tmp, ".")  ||  !strcmp(tmp, ".."))
  39.         isdir = 1;
  40.     else  {
  41.         r = stat(argv[argc-1], &sb);
  42.         isdir = !r  &&  (sb.st_mode & S_IFDIR);
  43.     }
  44.     argc--;
  45.     for ( ; 1 < argc  &&  argv[1][0] == '-' ; argc--, argv++)
  46.         for (r=1 ; argv[1][r] ; ++r)
  47.             switch (argv[1][r])  {
  48.             case 'e':
  49.             case 'E':
  50.                 existing = 1;
  51.                 break;
  52.             case 'f':
  53.             case 'F':
  54.                 force = 1;
  55.                 break;
  56.             case 'q':
  57.             case 'Q':
  58.                 quiet = 1;
  59.                 verbose = 0;
  60.                 break;
  61.             case 'v':
  62.             case 'V':
  63.                 verbose = 1;
  64.                 quiet = 0;
  65.                 break;
  66.             case 'z':
  67.             case 'Z':
  68.                 zero = 1;
  69.                 break;
  70.             default:
  71.                 fprintf(stderr, "mv:  Unknown flag %c\n", argv[i][r]);
  72.                 usage();
  73.                 break;
  74.             }
  75.     if (argc > 2  &&  !isdir  ||  argc < 2)
  76.         usage();
  77.     for (i=1 ; i < argc ; ++i)  {
  78.         r = stat(argv[i], &sb);
  79.         if (r)  {
  80.             if (!quiet)
  81.                 fprintf(stderr, "mv:  source file %s doesn't exist\n", argv[i]);
  82.             err = 1;
  83.             continue;
  84.         }
  85.         if ((sb.st_mode & S_IFDIR)  &&  isdir) {
  86.             if (!quiet)
  87.                 fprintf(stderr, "mv:  %s and %s are both directories\n", argv[i], argv[argc]);
  88.             err = 1;
  89.             continue;
  90.         }
  91.         if (isdir)  {
  92.             char    c;
  93.  
  94.             strip_path(tmp, argv[i]);
  95.             strcpy(buf, argv[argc]);
  96.             c = buf[strlen(buf)-1];
  97.             if (c != '/'  &&  c != '\\'  &&  c != ':')
  98.                 strcat(buf, "/");
  99.             strcat(buf, tmp);
  100.             if (!existing)
  101.                 rm(buf, force);
  102.             r = rename(argv[i], buf);
  103.             if (r)  {
  104.                 if (!quiet)
  105.                     fprintf(stderr, "mv:  Can't move %s to %s\n", argv[i], buf);
  106.                 err = 1;
  107.             } else if (verbose)
  108.                 fprintf(stderr, "Moving %s -> %s\n", argv[i], buf);
  109.         }  else  {
  110.             if (!existing)
  111.                 rm(argv[argc], force);
  112.             r = rename(argv[i], argv[argc]);
  113.             if (r)  {
  114.                 if (!quiet)
  115.                     fprintf(stderr, "mv:  Can't move %s to %s\n", argv[i], argv[argc]);
  116.                 err = 1;
  117.             } else if (verbose)
  118.                 fprintf(stderr, "Moving %s -> %s\n", argv[i], argv[argc]);
  119.         }
  120.     }
  121.     return zero ? 0 : err;
  122. }
  123.  
  124. static    void    usage()
  125. {
  126.     printf("Usage:\tmv  [-options]  file  file\n"); 
  127.     printf("\tmv  [-options]  file...  directory\n");
  128.     printf("Options:\n");
  129.     printf("\te\tdon't distroy pre-existing files\n");
  130.     printf("\tf\tforce (even if read only)\n");
  131.     printf("\tq\tquiet (no error messages)\n");
  132.     printf("\tv\tverbose\n");
  133.     printf("\tz\talways return 0 exist status\n");
  134.     exit(1); 
  135. }
  136.  
  137. static    void    strip_path(to, from)
  138. char    *from, *to;
  139. {
  140.     char    *t;
  141.  
  142.     for (t=from ; *t ; ++t)
  143.         if (*t == '/'  ||  *t == '\\'  ||  *t == ':')
  144.             from = t + 1;
  145.     strcpy(to, from);
  146. }
  147.  
  148. static    int    rm(f, force)
  149. char    *f;
  150. int    force;
  151. {
  152.     int    r;
  153.  
  154.     r = unlink(f);
  155.     if (r  &&  force)  {
  156.         _dos_setfileattr(f, _A_NORMAL);
  157.         r = unlink(f);
  158.     }
  159.     return(r);
  160. }
  161.  
  162.  
  163.  
  164.  
  165. /*
  166.  *
  167.  *    Copyright 1993 Algorithms Corporation
  168.  *
  169.  *    ALL RIGHTS RESERVED.
  170.  *
  171.  *
  172.  *
  173.  */
  174.  
  175.  
  176.